#include "gui.h"

#include <wx/wx.h>
#include <wx/statline.h>

enum
{
	wxID_BUTTON_CHANNEL_CLICK = wxID_HIGHEST + 1,
	wxID_BUTTON_CONNECT_CLICK,
	wxID_BUTTON_DISCONNECT_CLICK,
	wxID_BUTTON_MICON_CLICK,
	wxID_BUTTON_MICOFF_CLICK,
	wxID_SCROLL_TRACK
};

BEGIN_EVENT_TABLE(VCClientFrame,wxFrame)
	EVT_CLOSE(VCClientFrame::OnClose)
	EVT_BUTTON(wxID_BUTTON_CHANNEL_CLICK, VCClientFrame::OnClickButton)
	EVT_BUTTON(wxID_BUTTON_CONNECT_CLICK, VCClientFrame::OnClickButton)
	EVT_BUTTON(wxID_BUTTON_DISCONNECT_CLICK, VCClientFrame::OnClickButton)
	EVT_BUTTON(wxID_BUTTON_MICON_CLICK, VCClientFrame::OnClickButton)
	EVT_BUTTON(wxID_BUTTON_MICOFF_CLICK, VCClientFrame::OnClickButton)
	EVT_COMMAND_SCROLL_LINEUP(wxID_SCROLL_TRACK, VCClientFrame::OnTrackScroll)
	EVT_COMMAND_SCROLL_LINEDOWN(wxID_SCROLL_TRACK, VCClientFrame::OnTrackScroll)
	EVT_COMMAND_SCROLL_THUMBTRACK(wxID_SCROLL_TRACK, VCClientFrame::OnTrackScroll)
END_EVENT_TABLE()

IMPLEMENT_CLASS(VCClientFrame, wxFrame)

VCClientFrame::VCClientFrame(const wxString& title)
: wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE & ~(wxRESIZE_BORDER | wxMAXIMIZE_BOX))
, m_staticTextIp(NULL)
, m_textCtrlIp(NULL)
, m_staticTextPort(NULL)
, m_textCtrlPort(NULL)
, m_staticTextVolume(NULL)
, m_scrollBarVolume(NULL)
, m_staticTextChannel(NULL)
, m_textCtrlChannel(NULL)
, m_buttonChannel(NULL)
, m_buttonConnect(NULL)
, m_buttonDisconnect(NULL)
, m_buttonMicOn(NULL)
, m_buttonMicOff(NULL)
, m_vcclient(NULL)
{
	wxPanel * panelTop = new wxPanel(this, wxID_ANY);

	m_staticTextIp = new wxStaticText(panelTop, wxID_STATIC, wxT("Server IP:"));
	m_textCtrlIp = new wxTextCtrl(panelTop, wxID_ANY, wxT("127.0.0.1"));
	m_staticTextPort = new wxStaticText(panelTop, wxID_STATIC, wxT("Server port:"));
	m_textCtrlPort = new wxTextCtrl(panelTop, wxID_ANY, wxT("6100"));
	m_staticTextVolume = new wxStaticText(panelTop, wxID_STATIC, wxT("Volume:"));
	m_scrollBarVolume = new wxScrollBar(panelTop, wxID_SCROLL_TRACK);
	m_staticTextChannel = new wxStaticText(panelTop, wxID_STATIC, wxT("Channel:"));
	m_textCtrlChannel = new wxTextCtrl(panelTop, wxID_ANY, wxT("0"));
	m_buttonChannel = new wxButton(panelTop, wxID_BUTTON_CHANNEL_CLICK, wxT("Set channel"));
	m_buttonConnect = new wxButton(panelTop, wxID_BUTTON_CONNECT_CLICK, wxT("Connect"));
	m_buttonDisconnect = new wxButton(panelTop, wxID_BUTTON_DISCONNECT_CLICK, wxT("Disconnect"));
	m_buttonMicOn = new wxButton(panelTop, wxID_BUTTON_MICON_CLICK, wxT("Mic on"));
	m_buttonMicOff = new wxButton(panelTop, wxID_BUTTON_MICOFF_CLICK, wxT("Mic off"));

	wxSizer * sizerTop = new wxBoxSizer(wxVERTICAL);
	wxSizer * sizerAddr = new wxBoxSizer(wxHORIZONTAL);
	wxSizer * sizerVolume = new wxBoxSizer(wxHORIZONTAL);
	wxSizer * sizerChannel = new wxBoxSizer(wxHORIZONTAL);
	wxSizer * sizerButton = new wxBoxSizer(wxHORIZONTAL);

	sizerAddr->Add(m_staticTextIp, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, 5);
	sizerAddr->Add(m_textCtrlIp, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, 5);
	sizerAddr->Add(m_staticTextPort, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, 5);
	sizerAddr->Add(m_textCtrlPort, 0, wxALIGN_CENTER_VERTICAL | wxALL, 0);

	sizerVolume->Add(m_staticTextVolume, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, 5);
	sizerVolume->Add(m_scrollBarVolume, 1, wxALIGN_CENTER_VERTICAL | wxALL, 0);

	sizerChannel->Add(m_staticTextChannel, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, 5);
	sizerChannel->Add(m_textCtrlChannel, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, 5);
	sizerChannel->Add(m_buttonChannel, 0, wxALIGN_CENTER_VERTICAL | wxALL, 0);

	sizerButton->Add(m_buttonConnect, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, 5);
	sizerButton->Add(m_buttonDisconnect, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, 5);
	sizerButton->Add(m_buttonMicOn, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, 5);
	sizerButton->Add(m_buttonMicOff, 0, wxALIGN_CENTER_VERTICAL | wxALL, 0);

	sizerTop->Add(sizerAddr, 0, wxGROW | wxALL, 5);
	sizerTop->Add(sizerVolume, 0, wxGROW | wxALL, 5);
	sizerTop->Add(sizerChannel, 0, wxGROW | wxALL, 5);
	sizerTop->Add(sizerButton, 0, wxGROW | wxALL, 5);

	panelTop->SetSizer(sizerTop);
	sizerTop->Fit(this);
	sizerTop->SetSizeHints(this);

	m_buttonDisconnect->Disable();
	m_buttonMicOn->Disable();
	m_buttonMicOff->Disable();

	m_vcclient = VCNewClient();

	unsigned char volume = 0;
	if (m_vcclient)
	{
		volume = m_vcclient->GetVolume();
	}
	m_scrollBarVolume->SetScrollbar((int)volume, 1, 255, 1);
}

VCClientFrame::~VCClientFrame()
{
	if (m_vcclient)
	{
		VCDeleteClient(m_vcclient);
		m_vcclient = NULL;
	}
}

void VCClientFrame::OnClose(wxCloseEvent & evt)
{
	evt.Skip(true);
}

void VCClientFrame::OnTrackScroll(wxScrollEvent & evt)
{
	switch (evt.GetId())
	{
	case wxID_SCROLL_TRACK:
		{
			if (m_vcclient)
			{
				unsigned char volume = 0;
				volume = (unsigned char)m_scrollBarVolume->GetThumbPosition();
				m_vcclient->SetVolume(volume);
			}
		}
		break;
	default:
		{
		}
	}
}

void VCClientFrame::OnClickButton(wxCommandEvent & evt)
{
	switch (evt.GetId())
	{
	case wxID_BUTTON_CHANNEL_CLICK:
		{
			if (m_vcclient)
			{
				long channel = 0;
				if (m_textCtrlChannel->GetValue().ToLong(&channel))
				{
					m_vcclient->SetChannel((char)channel);
				}
			}
		}
		break;
	case wxID_BUTTON_CONNECT_CLICK:
		{
			if (m_vcclient)
			{
				unsigned long port = 0;
				if (m_textCtrlPort->GetValue().ToULong(&port) && (port != 0))
				{
					if (m_vcclient->Connect(m_textCtrlIp->GetValue().ToAscii(), (unsigned short)port))
					{
						m_buttonConnect->Disable();
						m_buttonDisconnect->Enable();
						m_buttonMicOn->Enable();
						m_textCtrlIp->Disable();
						m_textCtrlPort->Disable();
					}
				}
			}
		}
		break;
	case wxID_BUTTON_DISCONNECT_CLICK:
		{
			if (m_vcclient)
			{
				m_vcclient->Disconnect();
				m_buttonConnect->Enable();
				m_buttonDisconnect->Disable();
				m_buttonMicOn->Disable();
				m_buttonMicOff->Disable();
				m_textCtrlIp->Enable();
				m_textCtrlPort->Enable();
			}
		}
		break;
	case wxID_BUTTON_MICON_CLICK:
		{
			if (m_vcclient)
			{
				if (m_vcclient->MicOn())
				{
					m_buttonMicOn->Disable();
					m_buttonMicOff->Enable();
				}
			}
		}
		break;
	case wxID_BUTTON_MICOFF_CLICK:
		{
			if (m_vcclient)
			{
				m_vcclient->MicOff();
				m_buttonMicOn->Enable();
				m_buttonMicOff->Disable();
			}
		}
		break;
	default:
		{
		}
	}
}

